import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ReadImgRgb {
    public static int[][][] getImagePixel(String image) throws IOException {
        File file = new File(image);
        BufferedImage buffer;
        buffer = ImageIO.read(file);
        assert buffer != null;
        int width = buffer.getWidth();
        int height = buffer.getHeight();
        int minx = buffer.getMinX();
        int miny = buffer.getMinY();
        int[][][] matrix = new int[height][width][3];
        for (int j = miny; j < height; j++) {
            for (int i = minx; i < width; i++) {
                int pixel = buffer.getRGB(i, j);
                matrix[j][i][0] = (pixel & 0xff0000) >> 16;
                matrix[j][i][1] = (pixel & 0xff00) >> 8;
                matrix[j][i][2] = (pixel & 0xff);
            }
        }
        return matrix;
    }

    public static void reverse(int[][][] ori, int width, int height) {
        int start = 0;
        int end = width - 1;

        while (start < end) {
            for (int i = 0; i < 3; i++) {
                int temp = ori[height][start][i];
                ori[height][start][i] = ori[height][end][i];
                ori[height][end][i] = temp;
            }
            start++;
            end--;
        }
    }

    public static void getMatrixRgb(int[][][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int o = 1;
        for (int i = 0; i < m; i++) {
            if (i % 2 != 0) reverse(matrix, n, i);
            for (int j = 0; j < n; j++) {
                System.out.println("[part" + (o++) +"]");
                System.out.println("R=" + matrix[i][j][0]);
                System.out.println("G=" + matrix[i][j][1]);
                System.out.println("B=" + matrix[i][j][2]);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        getMatrixRgb(getImagePixel("C:\\Users\\Administrators\\Desktop\\rgb.png"));
    }
}